package com.example.communist_app_api.controller.web;

import com.example.communist_app_api.db.dto.PermissionDTO;
import com.example.communist_app_api.db.pojoDto.Action;
import com.example.communist_app_api.db.pojoDto.Module;
import com.example.communist_app_api.service.ActionService;
import com.example.communist_app_api.service.ModuleService;
import com.example.communist_app_api.service.PermissionService;
import com.example.communist_app_api.util.R;
import com.example.communist_app_api.vo.web.req.MenuCreateReq;
import com.example.communist_app_api.vo.web.req.MenuDeleteMenuMessageReq;
import com.example.communist_app_api.vo.web.req.MenuUpdateMessageReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author love~mlm
 * @version 1.0
 * Create by 2022/12/29 22:42
 */
@RestController
@RequestMapping("/permission/menu")
@Api("菜单controller控制器")
public class MenuController {
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private ModuleService moduleService;
    @Autowired
    private ActionService actionService;

    @PostMapping("/showMenu")
    @ApiOperation("显示所有菜单")
    public R showMenu() {
        List<PermissionDTO> permissionDTOList = permissionService.getMenuList();
        return R.ok().put("permissionDTOList", permissionDTOList);
    }

    @PostMapping("/create")
    @ApiOperation("新增")
    public R createSame(@RequestBody MenuCreateReq req) {
        //获取permissionId，新增一般新增的是下一级的
        //Integer permissionId = req.getPermissionId();
        Integer whichFloor = req.getWhichFloor();
        if (whichFloor == null ) {
            return R.error("菜单级别错误！");
        }
        if (whichFloor != 1){
            String permissionCode = req.getPermissionCode();
            //communist_manage_select，以第二个下划线为分割module_name_action_name
            String actionCode = permissionCode.substring(permissionCode.indexOf("_", permissionCode.indexOf("_") + 1) + 1);
            String moduleCode = permissionCode.substring(0, permissionCode.indexOf("_", permissionCode.indexOf("_") + 1));
            Module module = moduleService.getModuleByModuleName(moduleCode);
            Action action = actionService.getActionByActionName(actionCode);
            if (module == null) {
                return R.error("模块为空,请添加模块");
            }
            if (action == null) {
                return R.error("行为为空，请添加行为");
            }
        }

        Boolean flag = permissionService.createNextFloor(req);
        if (flag == true) {
            return R.ok("新增成功");
        } else {
            return R.error("新增失败");
        }
    }

    @PostMapping("/update")
    @ApiOperation("修改")
    public R updateMenuMessage(@RequestBody MenuUpdateMessageReq req) {
        Integer whichFloor = req.getWhichFloor();
        if (whichFloor == null) {
            return R.error("菜单级别错误！");
        }
        if (whichFloor != 1){
            String permissionCode = req.getPermissionCode();
            //communist_manage_select，以第二个下划线为分割module_name_action_name
            String actionCode = permissionCode.substring(permissionCode.indexOf("_", permissionCode.indexOf("_") + 1) + 1);
            String moduleCode = permissionCode.substring(0, permissionCode.indexOf("_", permissionCode.indexOf("_") + 1));
            Module module = moduleService.getModuleByModuleName(moduleCode);
            Action action = actionService.getActionByActionName(actionCode);
            if (module == null) {
                return R.error("模块为空,请添加模块");
            }
            if (action == null) {
                return R.error("行为为空，请添加行为");
            }
        }
        Boolean flag = permissionService.updateMenuMessage(req);
        if (flag == true) {
            return R.ok("修改成功");
        } else {
            return R.error("修改失败");
        }
    }

    @PostMapping("/delete")
    @ApiOperation("删除菜单")
    public R deleteMenuMessage(@RequestBody MenuDeleteMenuMessageReq req) {
        Boolean flag = permissionService.deleteMenuMessage(req);
        if (flag == true) {
            return R.ok("删除成功");
        } else {
            return R.error("删除失败");
        }

    }
}
